package com.pactera.demo.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pactera.demo.base.entity.LoginUser;
import com.pactera.demo.base.mapper.LoginUserMapper;
import com.pactera.demo.base.service.ILoginUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.core.CollectionUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 登录信息表 服务实现类
 * </p>
 *
 * @author mybatis.plus
 * @since 2020-02-27
 */
@Slf4j
@Service("loginUserService")
public class LoginUserServiceImpl extends ServiceImpl<LoginUserMapper, LoginUser> implements ILoginUserService {

    private LoginUserMapper loginUserMapper;

    @Override
    public UserDetails loadUserByUsername(String loginName) throws UsernameNotFoundException {
        LoginUser loginUser = loginUserMapper.selectByLoginName(loginName);
        log.info("loadUserByUsername -> loginName:{} -> {}", loginName,loginUser);
        if (loginUser == null) throw new UsernameNotFoundException("UserDetails does not exist!");
        Set<String> roleCodes = this.loginUserMapper.selectRolesByUserId(loginUser.getUserId());
        //todo 这里需要保证 set 不为 empty
        List<SimpleGrantedAuthority> simpleGrantedAuthorities = roleCodes.stream()
                .map(x -> new SimpleGrantedAuthority(x)).collect(Collectors.toList());
        loginUser.setAuthorities(simpleGrantedAuthorities);
        return loginUser;

    }

    @Override
    public Set<String> getRolesByUserId(String userId) {
        return this.loginUserMapper.selectRolesByUserId(userId);
    }

    /**
     * 用于第三方登录查询用户信息
     * @param userId
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        log.info("loadUserByUserId -> userId -> {}", userId);
        LoginUser loginUser = this.loginUserMapper.selectOne(new QueryWrapper<LoginUser>().lambda().eq(LoginUser::getUserId, userId));
        if(loginUser == null)throw new UsernameNotFoundException("UserDetails does not exist!");
        Set<String> roleCodes = this.loginUserMapper.selectRolesByUserId(userId);
        List<SimpleGrantedAuthority> simpleGrantedAuthorities = roleCodes.stream()
                .map(x -> new SimpleGrantedAuthority(x)).collect(Collectors.toList());
        loginUser.setAuthorities(simpleGrantedAuthorities);
        return loginUser;
    }

    public LoginUserServiceImpl(LoginUserMapper loginUserMapper) {
        this.loginUserMapper = loginUserMapper;
    }
}
